S = S || {};
S.Ct = S.Ct || {};
S.Node = S.Node || {};
S.Edge = S.Edge || {};
S.NodeUI = S.NodeUI || {};
S.Layout = S.Layout || {};

S.setStyle = function (e, style, prefix) {
    for (let prop in style) {
        let name = prefix ? prefix + "." + prop : prop, value = style[prop];
        if (name == 'label.size' || name == 'size' || (value instanceof Array) || (typeof value !== 'object')) {
            switch (prop) {
                case 'position':
                    S.setPosition(e, name, value);
                    break;
                case 'padding':
                    S.setInsets(e, name, value);
                    break;
                default:
                    e.setStyle(name, value);
            }
        } else {
            S.setStyle(e, value, name);
        }
    }
};

S.setOption = function (e, option) {
    for (var i in option) {
        switch (i) {
            case 'style':
                S.setStyle(e, option[i]);
                break;
            default:
                e[i] = option[i];
        }
    }
};

S.setPosition = function (e, name, value) {
    e.setStyle(name, Q.Position[value])
};

S.setInsets = function (e, name, value) {
    if (typeof value == "number") {
        e.setStyle(name, new Q.Insets(parseInt(value)));
    } else {
        let insets = value.split(' ');
        for (let i in insets) {
            insets[i] = parseInt(insets[i]);
        }
        switch (insets.length) {
            case 1:
                e.setStyle(name, new Q.Insets(insets[0]));
            case 2:
                e.setStyle(name, new Q.Insets(insets[0], insets[1]));
            case 3:
                e.setStyle(name, new Q.Insets(insets[0], insets[1], insets[2]));
            case 4:
                e.setStyle(name, new Q.Insets(insets[0], insets[1], insets[2], insets[3]));
        }
    }
};

S.apply = function (object, config) {
    if (object) {
        if (config && S.isObject(config)) {
            for (var i in config) {
                object[i] = config[i];
            }
        }
    }
    return object;
};

/**
 * 深度替换
 */
S.applyDepth = function (object, config) {
    if (object) {
        if (config && S.isObject(config)) {
            for (var i in config) {
                if (S.isObject(config[i])) {
                    object[i] = S.applyDepth(object[i], config[i]);
                } else {
                    object[i] = config[i];
                }
            }
        }
    }
    return object;
};

/**
 * applyIf
 */
S.applyIf = function (object, config) {
    if (object && config && S.isObject(config)) {
        for (var i in config) {
            if (object[i] === undefined) {
                object[i] = config[i];
            }
        }
    }
    return object;
};

S.isArray = function (object) {
    if (typeof Array.isArray === "function") {
        return Array.isArray(object);
    } else {
        return Object.prototype.toString.call(object) === "[object Array]";
    }
};

S.isObject = function (object) {
    return Object.prototype.toString.call(object) === "[object Object]";
};

